package cn.vendsystem.com.controller;


import cn.vendcommon.com.annotation.RequestLogin;
import cn.vendcommon.com.entity.constants.CommonConstants;
import cn.vendcommon.com.entity.Result;
import cn.vendcommon.com.entity.vo.TokenVO;
import cn.vendsystem.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class WxMinLoginController {


    @Autowired
    private UserService userService;

    /**
     * 小程序登录
     * @param token
     * @param ip
     * @param body
     * @return
     */
    @PostMapping("/wxMin")
    @RequestLogin
    public Result<TokenVO> login(
            @RequestHeader(value = CommonConstants.TOKEN_NAME, required = false) String token,
            @RequestHeader(value = CommonConstants.REAL_IP, required = false) String ip,
            @RequestBody Map<String, String> body){
        String phone = body.get("phone");
        String password = body.get("password");
        // 校验参数
        if (phone == null || password == null) {
            return Result.error("用户名或密码不能为空");
        }
        TokenVO tokenVO = userService.login(phone, password, ip, token);
        System.out.println(tokenVO);
        return Result.success(tokenVO);
    }

    /**
     * 微信登录
     * @param code
     * @return
     */
    @RequestLogin
    @GetMapping("/login/{code}")
    public Result<TokenVO> loginWX(@PathVariable String code) {
        return Result.success(userService.loginWX(code));
    }
}
